/**
 * @author helio
 */
if (!SALE.stocks) SALE.stocks = {}

if (!SALE.stocks.StocksBatchInsertPanel) {
	SALE.stocks.StocksBatchInsertPanel = Ext.extend(SALE.basicGridPanel, {
	
		layout: 'fit',
		border: false,
		initComponent: function(){
		
			this.initDetailsJson();
			
			this.detailsStore = new Ext.data.Store({
			    proxy: new Ext.ux.data.PagingMemoryProxy(this.detailsJson),
			    //remoteSort:true,
			    reader: new Ext.data.JsonReader({
					root: 'details',
					remoteSort: true,
			        fields: ['pcode', 'pname', 'punit', {name:'number',type:'float'}, 'sname', {name:'lower_limit',type:'float'}, {name:'upper_limit',type:'float'}]
			    })
			});
			this.detailsStore.load();
					
			// create the grid
			this.detailsGrid = new Ext.grid.EditorGridPanel({
				region:'center',
				store: this.detailsStore,
				viewConfig: {
			        forceFit: true,
					enableRowBody:true,
		            showPreview:true
			    },
				
				loadMask: true,
				stripeRows: true,
				border: true,
				autoExpandColumn: 'pname',
								
				columns: [new Ext.grid.RowNumberer(), {
	           		header: "商品编号",
	           		dataIndex: 'pcode',
	           		width: 60,
	           		editor: {
	                	xtype:'trigger',
	                    triggerClass: 'x-form-search-trigger',
	                    onTriggerClick: function(){
							var selectedRecord = this.detailsStore.getAt(this.selectedRowIndex);
		   					var condition = {querytype:1,queryvalue:selectedRecord.get('pcode')};
		   					SALE.window.showProductsSelectWindow(this.productsSelectedHandler.createDelegate(this),condition);
			       		}.createDelegate(this)
	                }
	       		},{
	           		header: "商品名称",
	           		dataIndex: 'pname',
	           		align: 'left',
	           		editor: {
	                	xtype:'trigger',
	                    triggerClass: 'x-form-search-trigger',
	                    onTriggerClick: function(){
	       					var selectedRecord = this.detailsStore.getAt(this.selectedRowIndex);
	       					var condition = {querytype:2,queryvalue:selectedRecord.get('pname')};
	       					SALE.window.showProductsSelectWindow(this.productsSelectedHandler.createDelegate(this),condition);
			       		}.createDelegate(this)
	                }
	        	}, {
					header: "仓库",
					dataIndex: 'sname',			
					width: 60,
					sortable: true,
					
					editor: new Ext.form.ComboBox({
						tpl: comboxTpl,
					    store: DataStoreUtil.getStorageStore(),
					    displayField:'name',
					    typeAhead: true,
					    mode: 'local',
					    forceSelection: false,
					    triggerAction: 'all',
					    selectOnFocus:true
					})
				}, {
	           		header: "数量",
	           		dataIndex: 'number',
	           		width: 70,
	           		align: 'right',
	           		//renderer: 'usMoney',
	           		editor: new Ext.form.NumberField({
	               		allowBlank: false,
	               		allowNegative: false,
	               		maxValue: 100000
	           		})
	        	},{
	           		header: "单位",
	           		dataIndex: 'punit',
	           		width: 50,
	           		align: 'right',
	           		editor: new Ext.form.TextField({
		               allowBlank: true
		           })
	        	},{
	           		header: "库存下限",
	           		dataIndex: 'lower_limit',
	           		width: 70,
	           		align: 'right',
	           		//renderer: 'usMoney',
	           		editor: new Ext.form.NumberField({
	               		allowBlank: true
	           		})
	        	},{
	           		header: "库存上限",
	           		dataIndex: 'upper_limit',
	           		width: 70,
	           		align: 'right',
	           		//renderer: 'cnMoney',
	           		editor: new Ext.form.NumberField({
	               		allowBlank: true
	           		})
	        	}],
				
				sm: new Ext.grid.CheckboxSelectionModel(),
				
				tbar:[{
		            text:'插入行',
		            tooltip:'插入行',
		            iconCls:'add',
					handler: this.insertOneRowHandler.createDelegate(this)
		        }, {
		            text:'删除行',
		            tooltip:'删除行',
		            iconCls:'remove',
					handler: this.deleteRowsHandler.createDelegate(this)
		        }, '->', '-', {
		            text:'全部入库',
		            tooltip:'全部入库',
		            iconCls:'save',
					handler: this.saveAllStocks.createDelegate(this)
		        }, '-'],
				
				
				listeners: {
					afteredit: this.aftereditHandler.createDelegate(this),
					cellclick : function(grid, rowIndex, columnIndex, e) {
						this.selectedRowIndex = rowIndex;
					}.createDelegate(this)
				}
			});
			
			this.items = this.detailsGrid;
			
			SALE.stocks.StocksBatchInsertPanel.superclass.initComponent.call(this);
		},
		
		initDetailsJson: function() {
			if(!this.detailsJson || !this.detailsJson.details) {
				this.detailsJson = {details: []};
			}
			if(this.detailsJson.details.length < 30) {
				for(var i = this.detailsJson.details.length; i < 30; i++) {
					this.detailsJson.details.push({
						pcode: "",
						pname: "",
						pnumber: "",
						punit: "",
						pprice: "",
						pamount: "",
						pcomment: ""
					});
				}
			}
		},
		
		slectedProductHandler : function(combox, record, index) {
			combox.value = record.data.name;		
			this.selectedProductInfo = record;
		},
		
		aftereditHandler : function(grid) {
			var record = grid.record;//获取被修改的行
		    var field = grid.field;//获取被修改的列
		         			
			if(this.selectedProductInfo && (field == "pcode" || field == "pname")) {
				record.set("pcode", this.selectedProductInfo.data.code);
				record.set("pname", this.selectedProductInfo.data.name);
				record.set("punit", this.selectedProductInfo.data.unit);
			}
		},
		
		productsSelectedHandler: function(record) {
			
			var selectedRecord = this.detailsStore.getAt(this.selectedRowIndex);
			selectedRecord.set('pcode',record.get('pcode'));
			selectedRecord.set("pname", record.get('pname'));
			selectedRecord.set("punit", record.get('punit'));
			//selectedRecord.set("pprice", record.get('pprice'));
		},
		
		saveAllStocks : function() {
			var store = this.detailsGrid.store;
			var records = store.getRange(0, store.getCount());
			
			var details = [];
			var index = 0;
			store.each(function(record){
				if(record.data.pcode && record.data.pcode != "") {
					details[index] = record.data;
					index ++;
				}		
			});
			
			SALE.Action.insertAllProductStocks(this.saveSuccessHandler.createDelegate(this), details);
		}
	});
	
	Ext.reg('stocksbatchinsert', SALE.stocks.StocksBatchInsertPanel);
}